<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function TreeNode(val, left, right) {
            this.val = (val === undefined ? 0 : val)
            this.left = (left === undefined ? null : left)
            this.right = (right === undefined ? null : right)
        }
        var reverseTree = function (root) {
            if (!root){
                return null
            }
            const right = root.right
            root.right = reverseTree(root.left)
            root.left = reverseTree(right)
            return root
        }
        let root = new TreeNode(1)
        root.left = new TreeNode(2)
        root.right = new TreeNode(3)
        root.right.right = new TreeNode(4)
        root.left.right = new TreeNode(5)
        console.log(reverseTree(root))
    </script>
</body>

</html>